﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;

namespace AOP.Core
{
    public class RemotingProxy<T>:RealProxy
    {
        #region target
        protected T Target
        {
            get;
            private set;
        }

        public IInterceptor Interceptor
        {
            get;
            private set;
        }
        #endregion

        #region constructors
        public RemotingProxy(T target)
            :base(typeof (T))
        {
            Target = target;
            Interceptor = new Interceptor();
        }

        public RemotingProxy(T target, IInterceptor interceptor)
        {
            Target = target;
            Interceptor = interceptor;
        }
        #endregion

        #region overrider Invoke
        public override IMessage Invoke( IMessage msg)
        {
            IMethodCallMessage callMessage = msg as IMethodCallMessage; 

            IInvocation invocation = new Invocation ();

            Interceptor.Intercept(invocation);

            return new ReturnMessage(invocation.MethodReturn.ReturnValue, new object[]{}, 0, null, callMessage);
        }
        #endregion
    }
}
